//
//  main.m
//  OC_省市区
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    NSString *filePath = @"/Users/dllo/Desktop/省市区/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@", str);
    NSArray *arr = [str componentsSeparatedByString:@"\n"];
//    NSLog(@"%@", arr);
    NSMutableArray *proviceArr = [NSMutableArray array];
    NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
    NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *temp = [arr objectAtIndex:i];
        if (![temp hasPrefix:@" "]) {

            provinceDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:temp, @"provinceName", nil];
                        [proviceArr addObject:provinceDic];
        } else if (![temp hasPrefix:@"   "]){
            NSMutableArray *cityArr = [NSMutableArray array];

            cityDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:temp, @"cityName", nil];
            [provinceDic setObject:cityArr forKey:temp];
                        [cityArr addObject:cityDic];
        } else {
            NSMutableArray *areaArr = [NSMutableArray array];
            [areaArr addObject:temp];
            [cityDic setObject:areaArr forKey:temp];
        }
            
    }
    
    // 输出
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:proviceArr options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *str1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSLog(@"%@", str1);
    
    
    
    
    
    return 0;
}
